package com.oracle.lingshangejia.model;

import android.content.Context;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.Converter;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.ObservableBody;
import com.oracle.lingshangejia.utils.SpUtils;

import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * @松 on 2019/6/25 14:22
 * 重新封装
 */
public class OkgoNetwork {





    public static <T> Observable<T> pos1t(Context context ,String url, Map<Object,Object> map,Class<T> tClass){
        String token = (String) SpUtils.get(context,"token", "");
        int mark= (int) SpUtils.get(context,"mark",0);
        map.put("token",token);
        map.put("mark",mark);
        JSONObject jsonObject = new JSONObject(map);
        return  Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(ObservableEmitter<T> emitter) throws Exception {
                okhttp3.Response response = OkGo.<String>post(url).tag(context).upJson(jsonObject.toString()).execute();
                Gson gson = new Gson();
                gson.fromJson(response.body().string(),tClass );
            }
        });


    }

    public static  Observable<String> post(Context context ,String url, Map<Object,Object> map){
        String token = (String) SpUtils.get(context,"token", "");
        int mark= (int) SpUtils.get(context,"mark",0);
        map.put("token",token);
        map.put("mark",mark);
        JSONObject jsonObject = new JSONObject(map);
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                okhttp3.Response  response = OkGo.<String>post(url).tag(context).upJson(jsonObject.toString()).execute();
                emitter.onNext(response.body().string());
            }
        });
    }
    public static String post(String url,String json) throws IOException {
        okhttp3.Response  response = OkGo.<String>post(url).upJson(json).execute();
        return  response.body().string();
    }

}
